package com.itheima.youdianda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.pojo.SysConfig;

import com.itheima.youdianda.pojo.SysDict;
import com.itheima.youdianda.service.SysConfigService;
import com.itheima.youdianda.service.SysDictService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sys_config")
@Slf4j
public class SysConfigController {
    @Autowired
    private SysConfigService service;
    //添加配置
    @PostMapping( "add")
    public R<String> add(@RequestBody SysConfig sysConfig){
        log.info("{}",sysConfig.toString());
        return service.add(sysConfig);

    }
    @GetMapping
    public void m(){
        System.out.println("1");
    }

   /* //返回所有配置数据
    @GetMapping("all")
    public R<List<SysConfig>> allConfig(){
        List<SysConfig> list = service.list();
        return R.success(list);
    }*/



    //返回所有配置数据
    @GetMapping("all")
    public R allConfig(){
        List<SysConfig> list1 = service.list();
        //根据配置种类来返回
        //获取配置类对应的table值
        List<String> typelist =typelist();
        //根据对应的类型来返回相应的配置
        List<HashMap> list = list(typelist);


        //allData数据
        List<alldata> alldata=allDat(list1);
        //configForm配置形式
        HashMap<String,String> configForm=configForm(list1);
        //configResult
        HashMap<String,String>configResult= configResult(list1);


        data a=new data();
        a.setList(list);
        a.setAllData(alldata);
        a.setConfigForm(configForm);
        a.setConfigRules(configResult);


        return R.success(a);
    }

    private HashMap<String,String> configResult(List<SysConfig> list1) {
        HashMap<String,String> configRules=new HashMap<>();
        String attrName="站点名称";
        for (SysConfig config : list1) {
            if (config.getAttrName().equals(attrName)){
                configRules.put(config.getAttrKey(),config.getValidator());
                break;
            }
        }
        return configRules;
    }

    private HashMap<String,String> configForm(List<SysConfig> list1) {
        HashMap<String,String>map=new HashMap<>();
        list1.stream().map(item->{
            map.put(item.getAttrKey(),item.getAttrValue());
            return item;
        }).collect(Collectors.toList());
        return map;
    }

    private List<alldata> allDat( List<SysConfig> list1 ) {

        List<alldata> alldataList = list1.stream().map(item -> {
            alldata alldata = new alldata();
            alldata.setId(item.getId());
            alldata.setAttr_key(item.getAttrKey());
            alldata.setAttr_value(item.getAttrValue());
            alldata.setValidator(item.getValidator());
            alldata.setConfig_value(item.getConfigValue());
            alldata.setType(item.getType());
            return alldata;
        }).collect(Collectors.toList());
        return alldataList;
    }

    //传来的是第一级目录配置信息
    private List<HashMap> list(List<String> typelist) {
       /* //根据
        List<Object> collect2 = typelist.stream().map(typeid -> {

            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysConfig::getTabValue, typeid);
            //每个对应的tablevalue集合
            List<SysConfig> list = service.list(wrapper);
            //获得每个具体的配置字符串...配置,二级配置信息组值集合
            List<String> collect1 = list.stream().map(item -> {
                return item.getGroupValue();
            }).distinct().collect(Collectors.toList());
            //设置小组值对应的数据
            List<HashMap> collect = collect1.stream().map(item -> {
                HashMap map = new HashMap();
                map.put("name", item);
                //顶一个个配置的集合
                List<SysConfig> jt = new ArrayList<>();
                list.stream().map(jt1 -> {
                    if (jt1.getGroupValue().equals(item)) {
                        jt.add(jt1);
                    }
                    return jt1;
                }).collect(Collectors.toList());
                map.put("data", jt);
                return map;
            }).collect(Collectors.toList());


            Object object = new Object();
            object.setChildren(collect);
            object.setName(list.get(0).getTabValue());
            object.setKey(list.get(0).getTabValue());
            return object;
        }).collect(Collectors.toList());*/
        //设置各个类型下的小组集合
        //typelist是系统配置，其他配置,基础配置
        List<HashMap> collect = typelist.stream().map(typeid -> {
            //对应的集合
            LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysConfig::getTabValue, typeid);
            List<SysConfig> grouplist = service.list(wrapper);
            //收集小组分类集合
            List<String> groups = grouplist.stream().map(type -> {
                return type.getGroupValue();
            }).distinct().collect(Collectors.toList());
            HashMap groupmap = new HashMap();
            //各个小组的对应系统配置
            List<HashMap> mapList = groups.stream().map(group -> {
                //小组的系统配置只出现一次
                LambdaQueryWrapper<SysConfig> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SysConfig::getGroupValue, group);
                List<SysConfig> list = service.list(wrapper1);
                //封装一层
                HashMap map = new HashMap();
                map.put("data", list);
                map.put("name", group);
                return map;
            }).collect(Collectors.toList());
            //在外面小组所有的配置
            groupmap.put("Object", mapList);
            groupmap.put("name", typeid);
            groupmap.put("key", typeid);


            //测试能添加数据吗；
            return groupmap;
        }).collect(Collectors.toList());


        return collect;
    }

    private List<String> typelist() {
        //所有的配置类
        List<SysConfig> list = service.list();
        List<String> collect = list.stream().map(item -> {
            return item.getTabValue();
        }).distinct().collect(Collectors.toList());
        return collect;
    }
    @Autowired
    private SysDictService sysDictService;


}
@Data
class Object{
    private String name;
    private String key;
    private Map children;
}
@Data
class data{
    private List<HashMap> list;
    private List<alldata> allData;
    private HashMap<String,String> configForm;
    private HashMap<String,String> configRules;
}
@Data
class alldata{
    private int id;
    private String attr_key;
    private String attr_value;
    private String validator;
    private String config_value;
    private int type;
}
